package br.urca.controller;

import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;

import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
import br.urca.dao.CursoTurnoDAO;
import br.urca.dao.SolicitacaoDAO;
import br.urca.model.CursoTurno;
import br.urca.model.Solicitacao;
import br.urca.utils.Util;

@Controller
public class CursoTurnoController {

	private CursoTurnoDAO mCursoTurnoDAO;		
	
	private Result result;

	@Inject
	public CursoTurnoController(CursoTurnoDAO mCursoTurnoDAO, Result result) {
		this.mCursoTurnoDAO = mCursoTurnoDAO;
		this.result = result;
	}
	
	public CursoTurnoController() { }

	@Get("/consulta/reconhecimento/{key},{cod}")
	public void consulta(String key, String cod) {
		
		CursoTurno ct = null;
		
		if (cod != null && key != null) {
			Properties properties = new Properties();
			String mCodCurso = null;
			String mKey = null;
			try {
				//pega somente o código do curso
				properties.load(new StringReader(cod));
				mCodCurso = properties.getProperty("cod");
				
				properties.load(new StringReader(key));
				mKey = properties.getProperty("key");
					
				if (mKey.equalsIgnoreCase(Util.KEY_ALUNO_ONLINE)) {
					ct = mCursoTurnoDAO.buscaPorId(mCodCurso);
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (ct != null) {
			this.result.use(Results.json()).from(ct,"resposta").serialize();
		} else {
			this.result.use(Results.json()).from(new Object(),"resposta").serialize();
		}
	}
}
